package sk.sodik.atomFilter.feeds;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * No, zda sa, ze tieto compound keys nefunguju...
 * 
 * FeedFilterId is representation of the key for the FeedFilter class.
 * 
 * @see FeedFilter
 */

public class FeedFilterId implements Serializable {
	public String userId;
	public String name;

	public FeedFilterId() {
	}

	public FeedFilterId(String value) {
		int i = value.indexOf("/");
		userId = value.substring(0, i);
		name = value.substring(i + 1);
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		return toString().equals(obj.toString());
	}

	@Override
	public int hashCode() {
		return toString().hashCode();
	}

	@Override
	public String toString() {
		return userId + "/" + name;
	}
	
	/**
	 * Encodes key to the URL safe representation. 
	 */
	public static String urlEncode(String key) {
		try {
			return URLEncoder.encode(key, "utf-8");
		} catch (UnsupportedEncodingException e) {
			// should not happen
			return key;
		}
	}
	
	/**
	 * Decodes key back from its URL safe representation
	 */
	public static String urlDecode(String key) {
		try {
			return URLDecoder.decode(key, "utf-8");
		} catch (UnsupportedEncodingException e) {
			//should not happen
			return key;
		} catch (IllegalArgumentException iae) {
			// can happen when illegal key is used.
			return key;
		}
	}

}
